﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EzDesk.Apps.Model.ProjectModule;
using EzDesk.Utility.ServiceStatusInfo;
using EzDesk.Utility.Web.Controllers;
using EzDesk.Utility.Web.Extensions;
using EzDesk.Utility.Web.ServiceReference;
using EzDesk.Utility.Json;

namespace EzDesk.Clients.DesktopWeb.Controllers
{
    public partial class ProjectController : EzDeskAppController
    {

        #region Page Methods

        public ActionResult ProjectTasks(Guid id)
        {
            dynamic model = new ExpandoObject();
            var project = ProjectService.GetFullProjectById(id).Result;
            project.TeamMembers = EzApiClient.GetSimpleUserInfos(project.MemberString);
            if (project.ChargePersonId.HasValue)
                project.Director = EzApiClient.GetSimpleUserById(project.ChargePersonId.Value);

            model.ProjectInfo = project;
            return View(model);
        }

        #endregion

        #region Ajax Methods
        /// <summary>
        /// 獲得項目Tasks
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetTasksInProject(Guid projectId)
        {
            var modelList = new List<dynamic> { };
            Action<Task, int> recursiveTaskTree = null;
            recursiveTaskTree = (task, deep) =>
            {
                //根据层级添加前置空格
                var preStr = deep == 0 ? "" : Enumerable.Repeat("&nbsp&nbsp&nbsp", deep).Aggregate((a, b) => a + b);
                modelList.Add(new
                {
                    Id = task.Id.ToString(),
                    Name = preStr + task.Name
                });
                task.ChildTasks.ToList().ForEach(t => recursiveTaskTree(t, deep + 1));
            };

            var tasks = ProjectService.GetPorjectTasks(projectId).Result.ToList();
            tasks.ForEach(t => recursiveTaskTree(t, 0));
            return Json(modelList, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 獲取Task的 workItems
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetWorkitemsInTask(Guid taskId)
        {
            var workitems = ProjectService.GetTaskWorkitems(taskId).Result.ToList();
            foreach (var workitem in workitems)
            {
                workitem.OwnerName = workitem.OwnerId.HasValue ? EzApiClient.GetSimpleUserById(workitem.OwnerId.Value).Name : " - ";
                workitem.CreatorName = EzApiClient.GetSimpleUserById(workitem.CreatorId).Name;
            }

            workitems = workitems
                .OrderByDescending(w => w.StatusValue)
                .OrderByDescending(w => w.CreateOn)
                .ToList();

            return Json(workitems, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public JsonResult AddNewTask(Guid projectId, string taskStr)
        {
            var task = WebJsonExtension.FromJson<Task>(taskStr);
            var statusInfo = ProjectService.CreateTask(task);

            if (statusInfo.Status == ServiceStatus.Success)
            {
                statusInfo.Info = "添加項目計劃成功";
            }
            return Json(statusInfo);
        }


        [HttpPost]
        public JsonResult UpdateTask(string taskStr)
        {
            var task = WebJsonExtension.FromJson<Task>(taskStr);

            var statusInfo = ProjectService.UpdateTask(task);
            if (statusInfo.Status == ServiceStatus.Success)
            {
                statusInfo.Info = "更新項目計劃成功";
            }

            return Json(statusInfo);
        }


        /// <summary>
        /// 刪除Task 
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DeleteTask(Guid taskId)
        {
            var statusInfo = ProjectService.DeleteTask(taskId);
            if (statusInfo.Status == ServiceStatus.Success)
            {
                statusInfo.Info = "刪除項目計劃成功";
            }

            return Json(statusInfo);
        }

        #endregion

        #region pravite methods

        #endregion
    }
}
